import javax.swing.*;
import javax.swing.event.MenuKeyEvent;
import javax.swing.event.MenuKeyListener;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.beans.PropertyChangeEvent;
import java.io.File;
import java.beans.PropertyChangeListener;
import java.io.FileNotFoundException;

/**
 * Created with IntelliJ IDEA.
 * User: Alexandre
 * Date: 13-10-14
 * Time: 14:05
 * To change this template use File | Settings | File Templates.
 */

public class Fenetre extends JFrame implements PropertyChangeListener {

    JLabel lblNbJeton, lblProgres;
    JTextField txtFichier, txtNbJetons;
    JProgressBar progressBar;
    JButton btnFichier, btnCompter, btnAnnuler;
    JMenuBar menuBar;
    JMenu menu;
    JMenuItem ouvrir, fermer;


    public Fenetre(){
        super("Compteur de jetons");
        //setDefaultLookAndFeelDecorated(true);

        createAndShowGui();
        setResizable(false);

    }

    private void init(final Container container){
        lblNbJeton = new JLabel("Nombre de jetons: ");
        lblProgres = new JLabel("Progrès: ");

        txtFichier = new JTextField();
        txtFichier.setEditable(false);
        txtNbJetons = new JTextField();
        txtNbJetons.setEditable(false);

        progressBar = new JProgressBar();
        btnFichier = new JButton("Choisir fichier");
        btnFichier.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {
                JFileChooser chooser = new JFileChooser();
                int returnVal = chooser.showOpenDialog(null);
                if(returnVal == JFileChooser.APPROVE_OPTION)
                    txtFichier.setText(chooser.getSelectedFile().getAbsolutePath());
            }
        });
        btnCompter = new JButton("Compter");
        btnCompter.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {
                if(txtFichier.getText().isEmpty())
                    return;
                else
                    actionCompter();
            }
        });
        btnAnnuler = new JButton("Annuler");
        btnAnnuler.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {
                actionAnnuler();
            }
        });
        btnAnnuler.setEnabled(false);

        Dimension btnSize = btnCompter.getPreferredSize();

        btnFichier.setSize(btnSize);
        txtFichier.setSize(txtFichier.getSize().width, btnSize.height);


        JPanel panelFichier = new JPanel();
        GridBagLayout topLayout = new GridBagLayout();
        panelFichier.setLayout(topLayout);
        GridBagConstraints c = new GridBagConstraints();
        c.fill = GridBagConstraints.HORIZONTAL;
        c.weightx = 4;
        c.gridx = 0;
        c.gridy = 0;
        c.gridwidth = 2;
        c.ipadx = 100;

        panelFichier.add(txtFichier, c);
        c.fill = GridBagConstraints.CENTER;
        c.weightx = 1;
        c.gridx = 2;
        c.gridwidth = 1;
        c.ipadx = 0;
        c.insets.left = 00;

        panelFichier.add(btnFichier, c);

        JPanel pane = new JPanel();
        pane.setLayout(new BoxLayout(pane, BoxLayout.PAGE_AXIS));
        pane.setBorder(BorderFactory.createEmptyBorder(10, 10, 10, 10));

        JPanel botPanel = new JPanel();
        GridLayout botLayout = new GridLayout(2, 3);
        botLayout.setHgap(10);
        botLayout.setVgap(10);

        botPanel.setLayout(botLayout);
        botPanel.add(lblNbJeton);
        botPanel.add(txtNbJetons);
        botPanel.add(btnCompter);
        botPanel.add(lblProgres);
        botPanel.add(progressBar);
        botPanel.add(btnAnnuler);

        pane.add(panelFichier);
        pane.add(Box.createRigidArea(new Dimension(0, 5)));
        pane.add(new JSeparator());
        pane.add(Box.createRigidArea(new Dimension(0, 5)));
        pane.add(botPanel);

        container.add(pane);


    }

    Worker worker;

    private void actionAnnuler() {
        btnCompter.setEnabled(true);
        btnAnnuler.setEnabled(false);
        txtNbJetons.setText("0");
        progressBar.setValue(0);
        worker.cancel(true);

    }

    private void actionCompter() {
        System.out.println("bouton compter");

        File file = new File(txtFichier.getText());
        worker = new Worker(file, this);
        btnAnnuler.setEnabled(true);
        worker.execute();

    }

    private void createAndShowGui() {
        setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

        menuBar = new JMenuBar();
        menu = new JMenu("Fichiers");
        menuBar.add(menu);
        ouvrir = new JMenuItem("Ouvrir");
        ouvrir.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {
                JFileChooser chooser = new JFileChooser();
                int returnVal = chooser.showOpenDialog(null);
                if(returnVal == JFileChooser.APPROVE_OPTION)
                    txtFichier.setText(chooser.getSelectedFile().getAbsolutePath());
            }
        });
        fermer = new JMenuItem("Fermer");
        fermer.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {
                System.exit(0);
            }
        });
        menu.add(ouvrir);
        menu.add(fermer);
        this.setJMenuBar(menuBar);
        init(getContentPane());
        pack();
        setVisible(true);
    }

    @Override
    public void propertyChange(PropertyChangeEvent evt) {
        if("compteur".equals(evt.getPropertyName())){
            CompteurDeJetons c = (CompteurDeJetons) evt.getSource();
            txtNbJetons.setText(String.valueOf(c.nJetons));
            progressBar.setValue(c.getProgres());
            progressBar.setStringPainted(true);
        }
        else if("done".equals(evt.getPropertyName())){
            if((Boolean)evt.getNewValue())
                progressBar.setValue(100);
        }

    }
}